package nl.nickthissen.remindme.activities;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import com.activeandroid.Model;

import nl.nickthissen.remindme.common.SaveResult;
import nl.nickthissen.remindme.data.Category;

/**
 * Created by nthissen on 11/06/13.
 */
public abstract class EditActivity<T extends Model> extends EditBaseActivity
{
    protected static final String ITEM_ID = "ITEM_ID";

    protected long getItemId()
    {
        Intent intent = this.getIntent();
        return intent.getLongExtra(ITEM_ID, 0);
    }

    @Override
    public void onDoneClicked()
    {
        SaveResult result = this.onSave();
        if (!result.success)
        {
            String message = result.message;
            if (TextUtils.isEmpty(message)) message = "Unknown problem occurred.";

            Toast.makeText(this, message, Toast.LENGTH_LONG).show();
            return;
        }

        this.setResult(RESULT_OK);
        onFinished();
    }

    @Override
    public void onCancelClicked()
    {
        this.setResult(RESULT_CANCELED);
        onFinished();
    }

    public abstract SaveResult onSave();
    public abstract void onFinished();
}
